<template>
  <div class="mvs_contaner padd100">
    <el-row :gutter="20">
      <el-col :lg="16" style="margin-bottom:20px">
        <div class="mvs_wrapper">
          <div class="mv_title">
            <el-tag>MV</el-tag>
            <h3>{{data.name}}</h3>
            <span v-for="(item,index) in data.artists">
              <span style="margin: 0 4px;color:#666" v-if="index != 0">/</span>
              <el-button link type="primary" @click="()=>{router.push({name:'artist-detail',params:{id:item.id}})}">
                {{item.name}}</el-button>
            </span>
          </div>
          <div class="mv_video">
            <videoPlay v-bind="options" @play="onPlay" @pause="onPause" @timeupdate="onTimeupdate"
              @canplay="onCanplay" />
          </div>
          <div class="esb_wrapper">
            <el-button :icon="Pointer" @click="getResourceLikeData" :style="{'color': mvinfo.liked ? '#409eff':'#000'}">
              ({{mvinfo.likedCount}})</el-button>
            <el-button :icon="data.subed ? Select:FolderAdd" @click="handleClickCollectionMv">收藏
              <span>({{data.subCount}})</span>
            </el-button>
          </div>
          <!-- 评论 -->
          <div id="comment">
            <div class="top_b_m_tit" style="margin-top:50px">
              <div class="left_tit">
                <div>评论</div>
                <span>共{{total}}条评论</span>
              </div>
            </div>
            <CommentComponent :isActiveId="false" :id="id" :playlistLimit="20" :pageNum="pageNum" @getTotal="getTotal"
              :commentType="4">
            </CommentComponent>
            <div class="pageination_center" style="margin-top:40px">
              <el-pagination background layout="prev, pager, next" :total="total" v-show="total > 0"
                v-model:currentPage="pageNum" :page-size="20" @current-change="handleCurrentChange" />
            </div>
          </div>
        </div>
      </el-col>
      <el-col :lg="8">
        <div class="right_other_container">
          <div class="mv_desc_wrapper">
            <div class="ohter_title">
              <span>MV简介</span>
            </div>
            <div class="mv_desc_content">
              <p class="text_muted">发布时间：{{data.publishTime}}</p>
              <p class="text_muted">播放次数：{{formatNumber(data.playCount)}}次</p>
              <p class="mv_desc">
                {{data.desc}}
              </p>
            </div>
          </div>
          <div class="mv_simi_wrapper">
            <div class="ohter_title">
              <span>相关推荐</span>
            </div>
            <div class="mv_simi_wrap">
              <div class="mv_item" v-for="item in mvs" :key="item.id">
                <div class="left_img cursor_class" @click="router.push({name:'mvs-detail',params:{id:item.id}})">
                  <img :src="item.cover + '?param=250y150'" alt="">
                  <span class="icon">
                    <el-icon :size="18" style="margin-right:4px;">
                      <VideoCamera />
                    </el-icon>
                    <span>{{formatNumber(item.playCount)}}</span>
                  </span>
                </div>
                <div class="right_info">
                  <div class="ti text_ellipsis text_decoration cursor_class"
                    @click="router.push({name:'mvs-detail',params:{id:item.id}})">{{item.name}}</div>
                  <div class="time text_ellipsis">{{dayjs(item.duration).format('mm:ss')}}</div>
                  <span class="author text_ellipsis">by <span class="cursor_class text_decoration"
                      @click="router.push({name:'artist-detail',params:{id:item.artistId}})">{{item.artistName}}</span></span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
  <!-- 登录注册弹窗 -->
  <LoginAndRegister ref="loginAndRegisterRef" />
</template>

<script lang="ts" setup>
import { isRef, onMounted, reactive, ref, toRefs, watch } from "vue"
import { Headset, Plus, Timer, VideoPlay, Comment, FolderAdd, Pointer, PriceTag, House, Sunny, Guide, MostlyCloudy, VideoCamera, Select } from '@element-plus/icons-vue'
import CommentComponent from "@/components/Comment/index"
import router from '@/router';
import { useRoute } from "vue-router";
import { ElMessage } from 'element-plus'
import dayjs from "dayjs";
import { getMvdetail, getMvVideo, getMvInfo, getSimiMv, getMvSub, getResourceLike } from "@/api/mvs";
import 'vue3-video-play/dist/style.css'
import { videoPlay } from 'vue3-video-play'
import { formatNumber } from "@/utils/index";
import { like } from "@/api/friend";
import LoginAndRegister from '@/components/LoginAndRegister/index'
import { loginAndRegisterTypeRef } from "@/types/login";
const loginAndRegisterRef = ref<loginAndRegisterTypeRef | null>()
const route = useRoute()
// 路由参数
let id = route.params.id
const pageNum = ref(1)
const total = ref(0)
const mvt = ref<number>(1)
const likeT = ref<number>(1)
const detailData = reactive<any>({
  data: { artists: {} }
})
const videoUrlObj = reactive<any>({
  urlObj: {}
})
const simiMvData = reactive<any>({
  mvs: { artists: {} }
})
const mvinfo = ref<any>({})
const { data } = toRefs(detailData)
const { urlObj } = toRefs(videoUrlObj)
const { mvs } = toRefs(simiMvData)
const options = reactive({
  width: '100%', //播放器高度
  height: '100%', //播放器高度
  color: "#409eff", //主题色
  muted: false, //静音
  webFullScreen: false,
  speedRate: ["0.75", "1.0", "1.25", "1.5", "2.0"], //播放倍速
  autoPlay: false, //自动播放
  loop: false, //循环播放
  mirror: false, //镜像画面
  ligthOff: false,  //关灯模式
  volume: 0.5, //默认音量大小
  control: true, //是否显示控制器
  title: '', //视频名称
  src: '', //视频源
  poster: '', //封面
  fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
})
const handleCurrentChange = (val: number) => {
  pageNum.value = val
}
const getTotal = (val: number) => {
  total.value = val
}
// 点击评论跳转指定位置
const handleClickScrollToComment = () => {
  document.getElementById('comment')?.scrollIntoView()
}
// 点击相似歌曲名字
const handleClickSimiSongName = (id: number) => {
  router.push({
    name: 'song',
    params: {
      id
    }
  })
}
const onPlay = (ev?: any) => {
  // console.log(ev)
  // let myElement: HTMLElement = document.querySelector('.icon-volume-mute') as HTMLElement;
  // console.log(myElement);
  // if (myElement) {
  //   myElement.click();
  // }
}
const onPause = (ev?: any) => {
  // console.log(ev, '暂停')
}

const onTimeupdate = (ev) => {
  // console.log(ev, '时间更新')
}
const onCanplay = (ev) => {
  // console.log(ev, '可以播放')
}
watch(() => router.currentRoute.value.path, (newValue, oldValue) => {
  console.log('路由变化了哦');
  // location.reload()
  id = route.params.id
  if (id && router.currentRoute.value.name === 'mvs-detail') {
    getMvdetailData()
    getMvVideoUrl()
    getMvInfoData()
    getSimiMvData()
  }
})
// 获取mv详情数据
const getMvdetailData = async () => {
  const res: any = await getMvdetail({ mvid: id })
  detailData.data = res.data
  options.poster = res.data.cover
}
//获取视频播放地址
const getMvVideoUrl = async () => {
  const res: any = await getMvVideo({ id })
  videoUrlObj.urlObj = res.data
  options.src = res.data.url
}
// 获取 mv 点赞转发评论数数据
const getMvInfoData = async () => {
  const res: any = await getMvInfo({ mvid: id })
  mvinfo.value = res
}
// 获取相似mv
const getSimiMvData = async () => {
  const res: any = await getSimiMv({ mvid: id })
  simiMvData.mvs = res.mvs
}
// 收藏
const handleClickCollectionMv = async () => {
  let cookie = localStorage.getItem('cookie')
  if (cookie) {
    if (data.value.subed) {
      mvt.value = 2
      data.value.subCount--
    } else {
      mvt.value = 1
      data.value.subCount++
    }
    const res: any = await getMvSub({
      mvid: id,
      t: mvt.value,
      cookie
    })
    if (mvt.value === 1) {
      data.value.subed = true
    } else {
      data.value.subed = false
    }
    ElMessage({
      message: res.message,
      type: 'success',
      duration: 2000,
    })
  } else {
    if (loginAndRegisterRef.value) {
      console.log(loginAndRegisterRef.value.loginVisible);
      loginAndRegisterRef.value.loginVisible = true
      loginAndRegisterRef.value.getQrKeyData()
    }
  }

  // getMvdetailData()
}
// 点赞
const getResourceLikeData = async () => {
  let cookie = localStorage.getItem('cookie')
  if (cookie) {
    let msg = "点赞成功"
    if (mvinfo.value.liked) {
      likeT.value = 2
      msg = "取消点赞成功"
      mvinfo.value.likedCount--
    } else {
      likeT.value = 1
      mvinfo.value.likedCount++
    }
    const res: any = await getResourceLike({
      id,
      type: 1,
      t: likeT.value,
      cookie
    })
    if (likeT.value === 1) {
      mvinfo.value.liked = true
    } else {
      mvinfo.value.liked = false
    }
    ElMessage({
      message: msg,
      type: 'success',
      duration: 2000,
    })
  } else {
    if (loginAndRegisterRef.value) {
      console.log(loginAndRegisterRef.value.loginVisible);
      loginAndRegisterRef.value.loginVisible = true
      loginAndRegisterRef.value.getQrKeyData()
    }
  }

  // getMvInfoData()
}
if (id) {
  getMvdetailData()
  getMvVideoUrl()
  getMvInfoData()
  getSimiMvData()
} else {
  ElMessage({
    message: "参数错误",
    type: 'error',
    duration: 2000,
    onClose: () => {
      history.go(-1)
    }
  })
}
onMounted(() => {
})
</script>
<style lang="scss" scoped>
.mvs_contaner {
  margin-top: 20px;

  .mvs_wrapper {
    .mv_title {
      display: flex;
      align-items: center;

      h3 {
        color: #333;
        font-size: 24px;
        margin: 0 10px;
        font-weight: normal;
      }
    }

    .mv_video {
      // width: 640px;
      height: 500px;
      // background: hotpink;
      margin: 20px 0;
    }
  }

  .right_other_container {
    .mv_desc_wrapper {
      .mv_desc_content {
        padding: 10px 0;

        p {
          margin: 5px 0;
        }

        .mv_desc {
          font-size: 12px;
          color: #333;
          line-height: 1.5;
        }
      }
    }

    .mv_simi_wrapper {
      margin: 20px 0;

      .mv_simi_wrap {
        padding: 20px 0;

        .mv_item {
          display: flex;
          overflow: hidden;
          align-items: center;
          margin-bottom: 20px;

          .left_img {
            width: 200px;
            height: 100px;
            position: relative;

            img {
              width: 100%;
              height: 100%;
              object-fit: cover;
            }

            .icon {
              position: absolute;
              right: 0;
              top: 0;
              color: #fff;
              display: flex;
              align-items: center;
              font-size: 14px;
              padding-right: 5px;
            }
          }

          .right_info {
            flex: 1;
            overflow: hidden;
            font-size: 12px;
            padding-left: 20px;

            .ti {
              color: #333;
            }

            .time {
              color: #999;
              margin: 10px 0;
            }

            .author {
              color: #999;
            }
          }
        }
      }
    }

    .ohter_title {
      font-weight: 700;
      font-size: 12px;
      color: #333;
      border-bottom: 1px solid #ccc;
      padding-bottom: 10px;
    }
  }
}

.top_b_m_tit {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 2px solid #409eff;
  padding-bottom: 10px;

  .left_tit {
    color: #333;
    font-size: 20px;
    display: flex;

    span {
      color: #666;
      font-size: 12px;
      margin-top: 10px;
      margin-left: 14px;
    }
  }

  .right_play_count {
    span {
      color: #666;
      font-size: 12px;

      span {
        color: #409eff;
        font-weight: 700;
      }
    }
  }
}
</style>
